<?php
session_start();
ob_start();
include("function.php");
condb();

$branch_id=$_GET['branch_id'];
$res=mysql_query("select * from course where course_id='$_GET[course_id]'");
$branch=mysql_fetch_assoc(mysql_query("select * from branch where branch_id='$branch_id'"));
if(mysql_num_rows($res)==0){redir('./');exit;}
$course=mysql_fetch_assoc($res);
?>
<meta charset="utf-8">
<center><b>คะแนนสอบรายวิชา<?php echo $course['course_name'];?></b><br />สาขาวิชา <?php echo $branch['branch_name'];?></center><br>

<?php
  $showtime=date('Y-m-d H:i:s',time()-600);
  $sql="select * from lesson where course_id='$_GET[course_id]' and exam_stop<='$showtime' and have_exam=1";
  $res=mysql_query($sql);
  $num=mysql_num_rows($res);
?>

<table width="100%" border="1" bordercolor="#000000">
  <tr>
    <td width="55" rowspan="2" align="center"><strong>ลำดับที่</strong></td>
    <td rowspan="2"><strong>ชื่อ-สกุล</strong></td>
    <td align="center" colspan="<?php echo $num;?>"><strong>คะแนนที่ได้</strong></td>
	<td align="center"><strong>รวม</strong></td>
  </tr>
  <tr>
  	<?php
	$total=0;
	while($lesson=mysql_fetch_assoc($res)){
		$total+=$lesson['question_num'];
	?>
    <td align="center"><?php echo "$lesson[lesson_name]<br>$lesson[exam_start]<br>($lesson[question_num])";?></td>
	<?php
	}
	$median=$total/2;
	?>
	<td align="center"><?php echo "($total)";?></td>
  </tr>
  <?php
	  $sql="select a.m_id,m_username,m_name,group_id,a.status_id from course_regis a,member b where a.m_id=b.m_id and course_id='$_GET[course_id]' and b.branch_id='$branch_id' order by m_name asc";
	  $res=mysql_query($sql);
	  $number=mysql_num_rows($res);
	  $i=0;
	  	while($row=mysql_fetch_assoc($res)){
			++$i;
	  		$class=($i%2!=0)?'rowA':'rowB';
			$status=mysql_fetch_assoc(mysql_query("select * from status where status_id='$row[status_id]'"));
			$groups=mysql_fetch_assoc(mysql_query("select * from groups where group_id='$row[group_id]'"));

			$sql="select * from lesson where course_id='$_GET[course_id]' and have_exam=1";
  			$res2=mysql_query($sql);
			$sum=0;
			while($lesson=mysql_fetch_assoc($res2)){
				$ex=mysql_fetch_assoc(mysql_query("select * from examination where lesson_id='$lesson[lesson_id]' and m_id='$row[m_id]'"));
				$sum+=(int)$ex['ex_score'];
			}
			$score[]=$sum;
			if(floor($sum)>floor($median))$class='green';
			if(floor($sum)==floor($median))$class='yellow';
			if(floor($sum)<floor($median))$class='red';
	?>
  <tr class="<?php echo $class;?>">
    <td width="55" align="center"><?php echo $i;?></td>
    <td><?php echo $row['m_name'];?></td>
	<?php
	$showtime=date('Y-m-d H:i:s',time()-600);
  	$sql="select * from lesson where course_id='$_GET[course_id]' and exam_stop<='$showtime' and have_exam=1";
  	$res2=mysql_query($sql);
	while($lesson=mysql_fetch_assoc($res2)){
		$ex=mysql_fetch_assoc(mysql_query("select * from examination where lesson_id='$lesson[lesson_id]' and m_id='$row[m_id]'"));
	?>
    <td align="right"><?php echo (int)$ex['ex_score'];?></td>
	<?php
	}
	?>
	<td align="right"><?php echo $sum;?></td>
  </tr>
  <?php
	  }
	  ?>
</table>
<?php
if($number>0){
?>
<br />
คะแนนต่ำสุด : <?php echo min($score);?><br />
คะแนนสูงสุด : <?php echo max($score);?><br />
ค่าเฉลี่ย : <?php echo number_format(array_sum($score)/count($score),2);?><br />
<br>
<center><input type="button" onClick="window.print();" value="พิมพ์หน้านี้"></center>
<?php
}
?>
<style>
.green td{ color:green;}
.yellow td{ color:#FF9933;}
.red td{ color:red;}
</style>